<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\ImageService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class ImageController extends BaseController
{
    //文件

    public function __construct(ImageService $service)
    {
        $this->db = $service;
    }

//    //列表
//    public function getList(Request $request)
//    {
//        $pages['start'] = $request->post('start', 1);
//        $pages['limit'] = $request->post('limit', 10);
//        $res = (new ImageService())->getList($pages);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson(0, '', $res['data'], $res['total']);
//    }

    //图片
    public function uploadImg(Request $request)
    {
//        $img = $request->post('img', '');
//        if (!$img) {
//            return $this->toJson(-2, '图片必须的');
//        }
//        $fpath = Upload::getPathByBase64($img);
        ini_set('upload_max_filesize', '100M');
        ini_set('post_max_size', '100M');
        $resUpload = Upload::uploadImg($request);
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, '上传失败');
        }
        $fpath = $resUpload['data'];
        $sizes = getimagesize($fpath);
        $res = $this->db->addImg($fpath, $sizes[0], $sizes[1]);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $arys = [
            'link' => env('DOMAIN') . '/' . $fpath,
            'w' => $sizes[0],
            'h' => $sizes[1],
        ];
        return $this->toJson(0, '', $arys);
    }

    //视频
    public function uploadVideo(Request $request)
    {
        ini_set('upload_max_filesize', '100M');
        ini_set('post_max_size', '100M');
        $resUpload = Upload::uploadVideo($request, 1, 1, time());
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, $resUpload['msg']);
        }
        $fpath = $resUpload['data'];
        $res = $this->db->addVideo($fpath);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $arys = [
            'link' => env('DOMAIN') . '/' . $fpath,
        ];
        return $this->toJson(0, '', $arys);
    }
}
